unit Music;

interface

uses
  Database,
  Windows, Messages, FileCtrl, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, MMSystem, ExtCtrls, MPlayer, Menus;

type
  TFormMusic = class(TForm)
    BtnOK: TBitBtn;
    BtnClose: TBitBtn;
    Files: TFileListBox;
    Dirs: TDirectoryListBox;
    Drives: TDriveComboBox;
    BtnSound: TBitBtn;
    LabelCount: TLabel;
    Label2: TLabel;
    MP: TMediaPlayer;
    PanelLoading: TPanel;
    Songlist: TListBox;
    BtnUp: TSpeedButton;
    BtnDown: TSpeedButton;
    MainMenu1: TMainMenu;
    MenuMain: TMenuItem;
    MenuUp: TMenuItem;
    MenuDown: TMenuItem;
    CtrlA1: TMenuItem;
    BtnStop: TBitBtn;
    BtnClear: TSpeedButton;
    BtnLoad: TBitBtn;
    BtnSave: TBitBtn;
    DialogOpenPlaylist: TOpenDialog;
    DialogSavePlaylist: TSaveDialog;
    BtnAutoload: TBitBtn;
    BtnClearList: TBitBtn;
    Bevel1: TBevel;
    Bevel2: TBevel;
    Bevel4: TBevel;
    Panel1: TPanel;
    Status: TLabel;
    Bevel3: TBevel;
    LabelChanged: TStaticText;
    BtnRemove: TBitBtn;
    BtnAdd: TBitBtn;
    procedure BtnCloseClick(Sender: TObject);
    procedure BtnSoundClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnOKClick(Sender: TObject);
    procedure FilesKeyPress(Sender: TObject; var Key: Char);
    procedure BtnUpClick(Sender: TObject);
    procedure BtnAddClick(Sender: TObject);
    procedure BtnRemoveClick(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure BtnStopClick(Sender: TObject);
    procedure BtnClearClick(Sender: TObject);
    procedure BtnLoadClick(Sender: TObject);
    procedure BtnSaveClick(Sender: TObject);
    procedure BtnAutoloadClick(Sender: TObject);
    procedure BtnClearListClick(Sender: TObject);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
      Y: Integer);
  private
    { Private declarations }
    procedure UpdateChanged;
    procedure UpdateAutoload;
    procedure UpdateCount;
  public
    { Public declarations }
    didchange, playlistchanged: boolean;
  end;

var
  FormMusic: TFormMusic;
  Songs: TStringList;
  loadedfile: string;

implementation

{$R *.DFM}

procedure TFormMusic.FormCreate(Sender: TObject);
var
  dir, f: string;
  i: integer;
begin
  loadedfile:='';
  PanelLoading.top:=281;
  PanelLoading.left:=32;
  dir:=Datawell.GetDir(5);
  if not DirectoryExists(dir) then dir:=datawell.getsounddir;
  if not DirectoryExists(dir) then dir:=datawell.exedir;
  dirs.directory:=dir;
  Songs:=TStringList.create;
  Songs.assign(Datawell.playlist);
  Songlist.items.clear;
  for i:=0 to Songs.count-1 do begin
    f:=extractfilename(Songs[i]);
    Songlist.items.add(f);
  end;
  didchange:=false;
  playlistchanged:=false;
  UpdateAutoload;
  UpdateCount;
end;

procedure TFormMusic.FormDestroy(Sender: TObject);
begin
  Songs.free;
end;

procedure TFormMusic.BtnCloseClick(Sender: TObject);
begin
  if playlistchanged then begin
    if messagedlg('The playlist has changed.  Do you still wish to exit?',
      mtinformation,[mbyes,mbno],0) = mrno then exit;
  end;
  didchange:=false;
  close;
end;

procedure TFormMusic.BtnOKClick(Sender: TObject);
begin
  if Songlist.items.count < 1 then begin
    messagedlg('You cannot load an empty playlist',mtinformation,[mbok],0);
    exit;
  end;
  if playlistchanged then begin
    if messagedlg('The playlist has not been saved.  Load it anyway?',
      mtinformation,[mbyes,mbno],0) = mrno then exit;
  end;
  Datawell.SetDir(5,dirs.directory);
  Datawell.playlist.assign(Songs);
  didchange:=true;
  close;
end;

procedure TFormMusic.FilesKeyPress(Sender: TObject; var Key: Char);
begin
  if key = #13 then begin
    key:=#0;
    BtnAdd.click;
  end
  else if key = #32 then begin
    key:=#0;
    BtnSound.click;
  end;
end;

procedure TFormMusic.BtnSoundClick(Sender: TObject);
var
  filename: string;
  didfail: boolean;
begin
  filename:=Files.filename;
  if not fileexists(filename) then exit;
  didfail:=false;
  MP.filename:=filename;
  PanelLoading.show;
  PanelLoading.refresh;
  try MP.open;
    except on EMCIDeviceError do didfail:=true;
  end;
  if not didfail then begin
    MP.notify:=true;
    MP.play;
  end;
  PanelLoading.hide;
  if (didfail) and (Datawell.TableGlobals.fieldbyname('ShowWarnings').asboolean) then
    messagedlg('That file is already opened by another media device or'+#13+
    'you are missing the device driver for the file: '+MP.filename+#13+
    'Please visit www.microsoft.com to download the latest Media Player drivers',
    mterror,[mbok],0);
end;

procedure TFormMusic.BtnUpClick(Sender: TObject);
var
  arg, i: integer;
  h: string;
begin
  arg:=(sender as TComponent).tag;
  i:=Songlist.itemindex;
  if i < 0 then exit;
  if (i = 0) and (arg < 0) then exit;
  if (i = (Songlist.items.count-1)) and (arg > 0) then exit;
  h:=Songlist.items[i+arg];
  Songlist.items[i+arg]:=Songlist.items[i];
  Songlist.items[i]:=h;
  Songlist.itemindex:=i+arg;
  h:=Songs[i+arg];
  Songs[i+arg]:=Songs[i];
  Songs[i]:=h;
  playlistchanged:=true;
  UpdateChanged;
end;

procedure TFormMusic.BtnRemoveClick(Sender: TObject);
begin
  if Songlist.itemindex < 0 then exit;
  if Songlist.items.count < 1 then exit;
  Songs.delete(Songlist.itemindex);
  Songlist.items.delete(Songlist.itemindex);
  playlistchanged:=true;
  UpdateChanged;
  UpdateCount;
end;

procedure TFormMusic.BtnAddClick(Sender: TObject);
begin
  if Songlist.items.count >= 100 then begin
    messagedlg('You can only load 100 files into a playlist',mtinformation,[mbok],0);
    exit;
  end;
  if Files.itemindex < 0 then exit;
  if length(Files.filename) > 254 then begin
    messagedlg('The filename is too long (must be under 255 chars)',mtinformation,[mbok],0);
    exit;
  end;
  if Songlist.itemindex >=0 then begin
    Songs.insert(Songlist.itemindex,Files.filename);
    Songlist.items.insert(Songlist.itemindex,Files.items[Files.itemindex]);
  end
  else begin
    Songlist.items.add(Files.items[Files.itemindex]);
    Songs.add(Files.filename);
  end;
  playlistchanged:=true;
  UpdateChanged;
  UpdateCount;
end;

procedure TFormMusic.BtnStopClick(Sender: TObject);
begin
  if MP.mode = mpplaying then MP.stop;
end;

procedure TFormMusic.BtnClearClick(Sender: TObject);
begin
  Songlist.items.clear;
  Songs.clear;
  playlistchanged:=true;
  UpdateChanged;
  UpdateCount;
end;

procedure TFormMusic.BtnSaveClick(Sender: TObject);
var f: string;
begin
  if Songlist.items.count < 1 then begin
    messagedlg('You cannot save an empty playlist',mtinformation,[mbok],0);
    exit;
  end;
  DialogSavePlaylist.initialdir:=Datawell.GetDir(5);
  if not DialogSavePlaylist.execute then exit;
  f:=DialogSavePlaylist.filename;
  Datawell.SetDir(5,f);
  if copy(f,length(f)-3,1) = '.' then f:=copy(f,1,length(f)-4);
  f:=f+'.tk3';
  Songs.savetofile(f);
  playlistchanged:=false;
  UpdateChanged;
end;

procedure TFormMusic.BtnLoadClick(Sender: TObject);
var
  i: integer;
  f: string;
  didfail: boolean;
begin
  if playlistchanged then begin
    if messagedlg('The playlist has changed.  Do you still wish to load a new one?',
      mtinformation,[mbyes,mbno],0) = mrno then exit;
  end;
  DialogOpenPlaylist.initialdir:=Datawell.GetDir(5);
  if not DialogOpenPlaylist.execute then
    exit;
  Datawell.SetDir(5,DialogOpenPlaylist.filename);
  didfail:=false;
  try
    Songs.loadfromfile(DialogOpenPlaylist.filename);
  except
    on EFOpenError do
      didfail:=true;
  end;
  if didfail then
    exit;
  loadedfile:=DialogOpenPlaylist.filename;
  while Songs.count > 100 do
    Songs.delete(Songs.count-1);
  Songlist.items.clear;
  for i:=0 to Songs.count-1 do begin
    f:=extractfilename(Songs[i]);
    Songlist.items.add(f);
  end;
  playlistchanged:=false;
  UpdateChanged;
  UpdateCount;
end;

procedure TFormMusic.BtnAutoloadClick(Sender: TObject);
begin
  DialogOpenPlaylist.initialdir:=Datawell.GetDir(5);
  if not DialogOpenPlaylist.execute then exit;
  if length(DialogOpenPlaylist.filename) > 254 then begin
    messagedlg('The filename is too long (must be under 255 chars)',mtinformation,[mbok],0);
    exit;
  end;
  Datawell.SetDir(5,DialogOpenPlaylist.filename);
  with Datawell.TableGlobals do begin
    edit;
    fieldbyname('S24').asstring:=DialogOpenPlaylist.filename;
    post;
    messagedlg('Playlist "'+fieldbyname('S24').asstring+'" marked for autoload',
      mtinformation,[mbok],0);
  end;
end;

procedure TFormMusic.UpdateChanged;
begin
  LabelChanged.visible:=playlistchanged;
end;

procedure TFormMusic.BtnClearListClick(Sender: TObject);
begin
  with Datawell.TableGlobals do begin
    edit;
    fieldbyname('S24').asstring:='';
    post;
    messagedlg('Auto-load playlist cleared',mtinformation,[mbok],0);
  end;
end;

procedure TFormMusic.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
  Status.caption:=' '+(sender as TControl).hint;
end;

procedure TFormMusic.UpdateAutoload;
begin
  BtnAutoload.hint:='Select autoload playlist';
  BtnClearList.hint:='Clears autoload playlist';
  if Datawell.TableGlobals.fieldbyname('S24').asstring <> '' then begin
    BtnAutoload.hint:=BtnAutoload.hint+' (currently: '+
      Datawell.TableGlobals.fieldbyname('S24').asstring+')';
    BtnClearList.hint:=BtnClearList.hint+' ('+
      Datawell.TableGlobals.fieldbyname('S24').asstring+')';
  end;
end;

procedure TFormMusic.UpdateCount;
var s: string;
begin
  s:='Music Playlist ('+inttostr(Songlist.items.count)+' Song';
  if Songlist.items.count <> 1 then s:=s+'s';
  s:=s+')';
  LabelCount.caption:=s;
end;

end.
